<style scoped>
.layui-form>>>.layui-form-select {
    z-index: inherit;
}
</style>

<template>
<div class="company">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>修改数据项</legend>
    </fieldset>

    <form class="layui-form ">
        <input type="hidden" name="guid" placeholder="" autocomplete="off" :value="guid" class="layui-input">
        <div class="layui-form-item">
            <label class="layui-form-label">抽检项名称</label>
            <div class="layui-input-block">
                <input type="text" name="itemname" :value="qaitems.itemname" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">最大可接受比率</label>
            <div class="layui-input-block">
                <input type="text" name="maxacctval" :value="qaitems.maxacctval" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分级标准</label>
            <div class="layui-input-block">
                <input type="hidden" name="qastandardname" ref="qastandardname" :value="qaitems.qastandardname" />
                <select name="qastandard" lay-verify="required" lay-filter="qastandard" lay-search>
                    <option value="">请选择</option>
                    <option v-for="(item, index) in standards" :value="item.guid" :selected="item.guid == qaitems.qastandard" :key="index">{{item.standard}}</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">质检标准</label>
            <div class="layui-input-block">
                <select id="qaMainGuid" name="qamainguid" lay-filter="qaMainGuid" lay-verify="required" lay-search="">
                    <option value="">请选择</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">排序</label>
            <div class="layui-input-block">
                <input type="text" name="listorder" :value="qaitems.listorder" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!--            <div class="layui-form-item">-->
        <!--                <label class="layui-form-label">状态 {{qaitems.status}}</label>-->
        <!--                <div class="layui-input-block">-->

        <!--                    <select   id="status"  name="status"  lay-filter="status" required  v-model="qaitems.status"   lay-search="">-->
        <!--                        <option value="1"   >启用</option>-->
        <!--                        <option value="0">禁用</option>-->
        <!--                    </select>-->

        <!--                </div>-->
        <!--            </div>-->

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="submit">立即提交</button>
                <button class="layui-btn layui-btn-primary" @click="$parent.back">返回</button>
            </div>
        </div>
        <div style="height: 500px"></div>
    </form>
</div>
</template>

<script>
export default {
    name: 'QaitemsEdit',
    props: ['id'],
    data() {
        return {
            guid: this.id,
            adata: {},
            qaitems: {},
            typeEmnu: [],
            costincomeEmnu: [],
            standards: '',
            standard: ''
        }
    },
    mounted() {
        let that = this;
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer;
            let loader = layer.load();
            //获取回显数据
            $.ajax({
                url: '/harvest/Qaitems!listVUEPC.action',
                data: {
                    guid: that.guid
                },
                type: "post",
                dataType: "json",
                success: function (res) {
                    if (res.info != "error") {
                        that.qaitems = res.info.rows[0];
                        var list = res.info.rows[0];
                        $.ajax({
                            url: '/trace/Qamain!get.action',
                            data: {},
                            type: "post",
                            dataType: "json",
                            success: function (res) {
                                console.log(res);
                                if (res.info != "error") {
                                    $.each(res, function (index, item) {
                                        if (item.qaname == that.qaitems.qaname) {
                                            $('#qaMainGuid').append("<option selected  value=" + item.guid + " > " + item.qaname + "</option>");
                                        } else {
                                            $('#qaMainGuid').append("<option   value=" + item.guid + " >" + item.qaname + "</option>");
                                        }
                                    });

                                    form.render('select');
                                } else {
                                    that.toast("系统错误");
                                }
                            },
                            complete: function () {
                                layer.close(loader);
                            }
                        });
                        form.render('select');
                    } else {
                        that.toast("系统错误");
                    }
                },
                complete: function () {
                    layer.close(loader);
                }
            });

            form.on('select(qastandard)', function (data) {
                let standard = that.standards.filter(item => {
                    return item.guid == data.value;
                })[0];
                that.$refs.qastandardname.value = standard.standard;
            });

            //提交编辑过的数据
            form.on('submit(submit)', function (data) {
                let loader = layer.load();
                $.ajax({
                    url: '/harvest/Qaitems!update.action',
                    data: data.field,
                    type: "post",
                    dataType: "json",
                    success: function (res) {
                        if (res.info == "success") {
                            that.toast("修改成功");
                            that.$parent.switchPage('Qaitems');
                        } else if (res.info == 'fail') {
                            that.toast("数据已经被引用，不允许修改！");
                        } else {
                            that.toast("系统错误");
                        }
                    },
                    complete: function () {
                        layer.close(loader);
                    }
                });
                return false;
            });
        });
        this.getStandards();
    },
    updated() {
        layui.form.render();
    },
    methods: {
        back: function () {
            this.$parent.switchPage(this.$parent.refView);
        },
        getqamain: function () {

        },
        getStandards: function () {
            let that = this;
            this.axios({
                method: 'get',
                responseType: 'json',
                url: '/harvest/Qastandard!getQastandards.action',
            }).then(response => {
                if (response.data.msg == "success") {
                    that.standards = response.data.standards;
                } else {
                    this.toastText('获取扣杂标准数据失败');
                }
            });
        }
    }
}
</script>
